package city.spring.modules.reception.controller;

import city.spring.modules.reception.entity.CheckInRecordEntity;
import city.spring.modules.reception.service.CheckInRecordService;
import city.spring.modules.reception.vo.CheckInRecordVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：入住记录
 *
 * @author 侯坤林
 * @date 2020-05-23 02:19:24
 */
@RestController
@RequestMapping("/check_in_record")
public class CheckInRecordController extends ApiController {
    private final CheckInRecordService checkInRecordService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CheckInRecordEntity, ?>> entityOrderFields;

    public CheckInRecordController(CheckInRecordService checkInRecordService) {
        this.checkInRecordService = checkInRecordService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CheckInRecordEntity::getId);
        entityOrderFields.add(CheckInRecordEntity::getOldmanId);
        entityOrderFields.add(CheckInRecordEntity::getCheckInTime);
        entityOrderFields.add(CheckInRecordEntity::getCheckOutTime);
        entityOrderFields.add(CheckInRecordEntity::getType);
        entityOrderFields.add(CheckInRecordEntity::getContractDate);
        entityOrderFields.add(CheckInRecordEntity::getContractNo);
        entityOrderFields.add(CheckInRecordEntity::getGmtCreate);
        entityOrderFields.add(CheckInRecordEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>入住记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CheckInRecordEntity entity) {
        LambdaQueryChainWrapper<CheckInRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(checkInRecordService.queryAll(lambdaQuery.getWrapper()));
    }

    /**
     * 分页获取 <strong>入住记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CheckInRecordEntity entity) {
        LambdaQueryChainWrapper<CheckInRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CheckInRecordVO> page = MyBatisUtils.toPage(pageable, false);
        return success(checkInRecordService.queryPage(page, lambdaQuery.getWrapper()));
    }

    /**
     * 获取一个 <strong>入住记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(checkInRecordService.getById(id));
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CheckInRecordEntity> buildLambdaQuery(CheckInRecordEntity entity) {
        LambdaQueryChainWrapper<CheckInRecordEntity> lambdaQuery = checkInRecordService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CheckInRecordEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getOldmanId()), CheckInRecordEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.eq(entity.getCheckInTime() != null, CheckInRecordEntity::getCheckInTime, entity.getCheckInTime());
        lambdaQuery.eq(entity.getCheckOutTime() != null, CheckInRecordEntity::getCheckOutTime, entity.getCheckOutTime());
        lambdaQuery.eq(entity.getType() != null, CheckInRecordEntity::getType, entity.getType());
        lambdaQuery.eq(entity.getContractDate() != null, CheckInRecordEntity::getContractDate, entity.getContractDate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContractNo()), CheckInRecordEntity::getContractNo, entity.getContractNo());
        lambdaQuery.eq(entity.getGmtCreate() != null, CheckInRecordEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, CheckInRecordEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 根据老人id查找老人最近的一条入住登记记录
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("/getLatestRecordByOldmanId/{oldmanId}")
    public Object getLatestRecordByOldmanId(@PathVariable String oldmanId) {
        return success(checkInRecordService.getLatestRecordByOldmanId(oldmanId));
    }

}
